#!/usr/bin/env python

import time
import platform
import os

class Commands:
    _minspace=30
    
    def __init__(self):
        self.doc={}
        self.short={}
        self.full={}
        self.callMain=True
        l=dir(self)
        for ind in l:
            if ind[0:2]=="__" or ind.find("_Commands")==0 or ind=="command" or ind=="usage" or ind=="main" or ind=="help":
                continue
            if ind[0]=="_":
                self.__createShortName(ind[1:])
            
    def __createShortName(self,name,length=1):
        n=name[0:length]
        if self.full.has_key(n):
            return self.__createShortName(name,length+1)
        self.full[n]=name
        self.short[name]=n
        return n
        
    def __getShortName(self,name):
        #if not self.short.has_key(name):
        #    self.short[name]=self.__createShortName(name)
        return self.short[name]
        
    def __getFullName(self,name):
        return self.full[name]
    
    def command(self,argv):
        
        t=time.time()
        options=False
        args=[]
        argv.reverse()
        stop=False
        for cmd in argv:
            if cmd[0]=="-":
                options=True
                if cmd[1]=="-":
                    cmd=cmd[2:]
                else:
                    cmd=self.__getFullName(cmd[1:])
                stop=self.__command("_"+cmd,args)
                args=[]
            elif hasattr(self,cmd):
                stop=self.__command(cmd,args)
                args=[]
            else:
                args.append(cmd) 
            if stop:
                return
            
        if self.__command("main",args):
            self.__command("_help")
            
        #print ""
        #print "COMPLETE"
        #print ">Time:"+str(time.time()-t)+"s"
        
    def __command(self,cmd,arg=[]):
        if hasattr(self,cmd):
            cmd=getattr(self,cmd)
            return cmd(*arg)
     
    def help(self):
        return "override def usage(self):"
        
    def _help(self):
        "show this help"
        result=""
        if hasattr(self,"main"):
            obj=getattr(self,"main")
            doc=obj.im_func.func_doc
            if doc:
                result+=doc+"\n"
        result+=self.help()
        result+="\n\n"
        l=dir(self)
        
        
        
        
        result+="\nOPTIONS"
        result+=' '*(self._minspace-len("OPTIONS"))+"HELP\n"
        for ind in l:
            if ind[0:2]=="__" or ind.find("_Commands")==0 or ind=="command" or ind=="help" or ind=="main":
                continue
            obj=getattr(self,ind)
            
            doc="unknown"
            if hasattr(obj,"im_func"):
                doc=str(obj.im_func.func_doc)
                args=obj.func_code.co_varnames
                if ind[0]=="_":
                    ind=ind[1:]
                    ind="--"+ind+", -"+self.__getShortName(ind)
                    for arg in args[1:obj.func_code.co_argcount]:
                        ind+=" "+arg
                result+=""+str(ind)+' '*(self._minspace-len(ind)-2)+"- "+doc+"\n"
        print result
        if platform.release()=="Vista":
            print "VISTA USER: you need to use: python.exe file.py"
        return True
    
    